/* $RCSFile: ADNDynContract.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynContract.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:48  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:32  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynContract : DynEntity
	{
        public enum ContractAllotmentTypeCode: int {Case=1,Time=2,CoverageDates=3};

        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.contract)
            {}

            public override Type DynType
            { get { return typeof(ADNDynContract); } }

            public override string DynTypename
            {   get{ return CrmStr.contract;}}

            public override DynEntity Build()
            {   return new ADNDynContract();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynContract(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynContract(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynContract(w,PrimaryKeyValue);}
        }

        static ADNDynContract(){new Ftor();} 

        public override bool HasOwningBusinessUnitProperty
        {
            get{   return true;}
        }

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynContract(): base()
		{}

        public ADNDynContract(CRMWrapperBase w,string contractTitle)
			: base(w,CrmStr.contract)
		{
            if ( contractTitle != string.Empty )
                title    =   contractTitle;
		}

		public ADNDynContract(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.contract, CrmId)
		{}

		public ADNDynContract(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.contract,e,null,null)
		{}

		public ADNDynContract(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
           : base(w,CrmStr.contract,e,prop,s)
		{}

        public ADNDynContract(ResetWrapperHandler fn,string contractTitle)
			: base(fn,CrmStr.contract)
		{
            if ( contractTitle != string.Empty )
                title    =   contractTitle;
		}

		public ADNDynContract(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.contract, CrmId)
		{}

		public ADNDynContract(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.contract,e,null,null)
		{}

		public ADNDynContract(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
           : base(fn,CrmStr.contract,e,prop,s)
		{}

        [CrmDynAttribute(CrmStr.contract)]
        public Guid contractid
		{
			get
			{
				return KeyPropertyValue(CrmStr.contractid);
			}
			set
			{
				AddKeyProperty(CrmStr.contractid, value);
			}
		}
        
        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.contractid) || KeyPropertyValue(CrmStr.contractid) != crmId )
                contractid  =   crmId;
        }

        public override string PrimaryFieldName
        {
            get
            {
               return CrmStr.title;
            }
           set
           {}
       }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return title;
            }
            set
            {
                title =   value;
            }
        }

        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return contractid;
           }
           set
           {
               contractid    =   value;
           }
        }

        /// <summary>
        /// Gets or sets the title of the contract. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string title 
        {
            get
            {
                return StringPropertyValue(CrmStr.title);
            }
            set
            {
                AddStringProperty(CrmStr.title,value);
            }
        }

    
        /// <summary>
        /// Gets or sets the description of the contract. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string contractlanguage 
        {
            get
            {
                return StringPropertyValue(CrmStr.contractlanguage);
            }
            set
            {
                AddStringProperty(CrmStr.contractlanguage,value);
            }
        }

        /// <summary>
        /// Gets or sets the system-generated contract identification number. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string contractnumber 
        {
            get
            {
                return StringPropertyValue(CrmStr.contractnumber);
            }
            set
            {
                AddStringProperty(CrmStr.contractnumber,value);
            }
        }

        /// <summary>
        /// Gets or sets the days of the week and times during which customer service support is available for the duration of the contract. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string effectivitycalendar 
        {
            get
            {
                if ( ContainsKey(CrmStr.effectivitycalendar) )
                    return StringPropertyValue(CrmStr.effectivitycalendar);
                return string.Empty;
            }
            set
            {
                AddStringProperty(CrmStr.effectivitycalendar,value);
            }
        }

        /// <summary>
        /// Gets the calculated duration of the contract in days. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public int duration
        {
            get
            {
                if ( ContainsKey(CrmStr.duration) )
                    return NumberPropertyValue(CrmStr.duration);
                return 0;
            }
            set
            {
                AddNumberProperty(CrmStr.duration,value);
            }
        }
    
        /// <summary>
        /// Gets or sets the date and time when the contract becomes active. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string activeon
        {
            get
            {
                if ( ContainsKey(CrmStr.activeon) )
                    return DateTimePropertyValue(CrmStr.activeon);
                return string.Empty;
            }
            set
            {
                AddDateTimeProperty(CrmStr.activeon,value);
            }
        }

        /// <summary>
        /// Gets or sets the date when the contract expires. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string expireson
        {
            get
            {
                if ( ContainsKey(CrmStr.expireson) )
                    return DateTimePropertyValue(CrmStr.expireson);
                return string.Empty;
            }
            set
            {
                AddDateTimeProperty(CrmStr.expireson,value);
            }
        }

        /// <summary>
        /// Gets the date when the contract was canceled. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string cancelon
        {
            get
            {
                if ( ContainsKey(CrmStr.cancelon) )
                    return DateTimePropertyValue(CrmStr.cancelon);
                return string.Empty;
            }
            set
            {
                AddDateTimeProperty(CrmStr.cancelon,value);
            }
        }

        /// <summary>
        /// Gets or sets the ID of the original contract from which this current contract was derived. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public Guid originatingcontract
        {
			get
			{
                if ( ContainsKey(CrmStr.originatingcontract) )
				    return LookupPropertyValue(CrmStr.originatingcontract);
                return Guid.Empty;
			}
			set
			{
				AddLookupProperty(CrmStr.originatingcontract,CrmStr.contract, value);
			}
        }

        public string originatingcontractName
        {
            get
            {
                if ( ContainsKey(CrmStr.originatingcontract) )
                    return LookupPropertyName(CrmStr.originatingcontract);
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the ID of the address at which service is to be provided. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public Guid serviceaddress
        {
			get
			{
                if ( ContainsKey(CrmStr.serviceaddress) )
				    return LookupPropertyValue(CrmStr.serviceaddress);
                return Guid.Empty;
			}
			set
			{
				AddLookupProperty(CrmStr.serviceaddress,CrmStr.customeraddress, value);
			}
        }

        public string serviceaddressName
        {
            get
            {
                if ( ContainsKey(CrmStr.serviceaddress) )
                    return LookupPropertyName(CrmStr.serviceaddress);
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the type of allotment that the contract supports. 
        /// Number of Cases: default
        /// Time
        /// Coverage Dates        
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public int allotmenttypecode 
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.allotmenttypecode);
            }
            set
            {
                AddPicklistProperty(CrmStr.allotmenttypecode,value);
            }
        }

        public string allotmenttypecodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.allotmenttypecode);
            }
        }

        /// <summary>
        /// Gets or sets the response level or level of service specified for the contract.         
        /// Gold
        /// Silver
        /// Bronze
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public int contractservicelevelcode 
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.contractservicelevelcode);
            }
            set
            {
                AddPicklistProperty(CrmStr.contractservicelevelcode,value);
            }
        }

        public string contractservicelevelcodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.contractservicelevelcode);
            }
        }

        #region Contract template
        
        /// <summary>
        /// Gets the abbreviation of the contract template name. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string contracttemplateabbreviation
        {
            get
            {
                return StringPropertyValue(CrmStr.contracttemplateabbreviation);
            }
            set
            {
                AddStringProperty(CrmStr.contracttemplateabbreviation,value);
            }
        }
     
        /// <summary>
        /// Gets or sets the ID of the template associated with the contract. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public Guid contracttemplateid
        {
			get
			{
                if ( ContainsKey(CrmStr.contracttemplateid) )
    				return LookupPropertyValue(CrmStr.contracttemplateid);
                return Guid.Empty;
			}
			set
			{
				AddLookupProperty(CrmStr.contracttemplateid,CrmStr.contracttemplate, value);
			}
        }

        [CrmDynAttribute(CrmStr.contract)]
        public string contracttemplateName
        {
            get
            {
                if ( ContainsKey(CrmStr.contracttemplateid) )
                    return LookupPropertyName(CrmStr.contracttemplateid);
                return string.Empty;
            }
        }

        #endregion Contract template

        #region customer

        string _DefaultCustomerName   =   CrmStr.account;
		public string DefaultCustomerName
		{
			get
			{
				return _DefaultCustomerName;
			}
			set
			{
                switch (value)
                {
                    case CrmStr.account:
                    case CrmStr.contact:
                        _DefaultCustomerName  =   value;
                        break;
                }
			}
		}

        /// <summary>
        /// Gets or sets the ID of the account or contact associated with the contract. 
        /// Beware : set defaults to account, to change it set DefaultCustomerName property before calling set
        /// or use SetContactCustomerid for adding contacts
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public Guid customerid 
        {
            get 
            {
                return CustomerPropertyValue(CrmStr.customerid);
            }
            set 
            {
                AddCustomerProperty(CrmStr.customerid,DefaultCustomerName,value);
            }
        }

        /// <summary>
        /// The entity : account or contact
        /// </summary>
        public string customerType
        {
            get 
            {
                return CustomerPropertyType(CrmStr.customerid);
            }
        }

         /// <summary>
        /// To adding contact customerids
        /// </summary>
        public void SetContactCustomerid(Guid contactid)
        {
            AddCustomerProperty(CrmStr.customerid,CrmStr.contact,contactid);
        }

        #endregion customer

        #region Billing

        string _DefaultBillingCustomerName   =   CrmStr.account;
		public string DefaultBillingCustomerName
		{
			get
			{
				return _DefaultBillingCustomerName;
			}
			set
			{
                switch (value)
                {
                    case CrmStr.account:
                    case CrmStr.contact:
                        _DefaultBillingCustomerName  =   value;
                        break;
                }
			}
		}

        /// <summary>
        /// Gets or sets the ID of the account or contact to which the contract is to be billed. 
        /// Beware : set defaults to account, to change it set DefaultCustomerName property before calling set
        /// or use SetContactCustomerid for adding contacts
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public Guid billingcustomerid 
        {
            get 
            {
                if ( ContainsKey(CrmStr.billingcustomerid) )
                    return CustomerPropertyValue(CrmStr.billingcustomerid);
                return Guid.Empty;
            }
            set 
            {
                AddCustomerProperty(CrmStr.billingcustomerid,DefaultBillingCustomerName,value);
            }
        }

        /// <summary>
        /// The entity : account or contact
        /// </summary>
        public string BillingCustomerType
        {
            get 
            {
                if ( ContainsKey(CrmStr.billingcustomerid) )
                    return CustomerPropertyType(CrmStr.billingcustomerid);
                return string.Empty;
            }
        }

         /// <summary>
        /// To adding contact customerids
        /// </summary>
        public void SetBillingContactCustomerid(Guid contactid)
        {
            AddCustomerProperty(CrmStr.billingcustomerid,CrmStr.contact,contactid);
        }

        /// <summary>
        /// Gets or sets the date and time when the billing period begins. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string billingstarton
        {
            get
            {
                if ( ContainsKey(CrmStr.billingstarton) )
                    return DateTimePropertyValue(CrmStr.billingstarton);
                return string.Empty;
            }
            set
            {
                AddDateTimeProperty(CrmStr.billingstarton,value);
            }
        }

        /// <summary>
        /// Gets or sets the date and time when the billing period ends. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public string billingendon
        {
            get
            {
                if ( ContainsKey(CrmStr.billingendon) )
                    return DateTimePropertyValue(CrmStr.billingendon);
                return string.Empty;
            }
            set
            {
                AddDateTimeProperty(CrmStr.billingendon,value);
            }
        }
        
        /// <summary>
        /// Gets or sets how often the customer or account is to be billed. 
        /// Monthly
        /// Bimonthly
        /// Quarterly
        /// Semiannually
        /// Annually        
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public int billingfrequencycode 
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.billingfrequencycode);
            }
            set
            {
                AddPicklistProperty(CrmStr.billingfrequencycode,value);
            }
        }

        /// <summary>
        /// Gets the priority name of the order.
        /// High 2 Specifies high priority. 
        /// Low 0 Specifies low priority. 
        /// Medium 1 Specifies medium priority. 
        /// </summary>
        public string billingfrequencycodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.billingfrequencycode);
            }
        }
         
        /// <summary>
        /// Gets or sets the address to bill for contract charges. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public Guid billtoaddress
        {
			get
			{
                if ( ContainsKey(CrmStr.billtoaddress) )
				    return LookupPropertyValue(CrmStr.billtoaddress);
                return Guid.Empty;
			}
			set
			{
				AddLookupProperty(CrmStr.billtoaddress,CrmStr.customeraddress, value);
			}
        }

        public string billtoaddressName
        {
            get
            {
                if ( ContainsKey(CrmStr.billtoaddress) )
                    return LookupPropertyName(CrmStr.billtoaddress);
                return string.Empty;
            }
        }

        #endregion Billing

        #region currency

        /// <summary>
        /// Gets the exchange rate for the currency associated with the product with respect to the base currency.
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public decimal exchangerate
        {
            get
            {
                return DecimalPropertyValue(CrmStr.exchangerate);
            }
            set
            {
                AddDecimalProperty(CrmStr.exchangerate,value);
            }
        }

        /// <summary>
        /// Gets and sets the ID of the currency associated with the product.         
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public Guid transactioncurrencyid
        {
            get
            {
                return LookupPropertyValue(CrmStr.transactioncurrencyid);
            }
            set
            {
                AddLookupProperty(CrmStr.transactioncurrencyid,CrmStr.transactioncurrency,value);
            }
        }

        public string transactioncurrencyName
        {
            get
            {
                return LookupPropertyName(CrmStr.transactioncurrencyid);
            }
        }

        #endregion currency
        
        #region Pricing
        
        /// <summary>
        /// Gets the sum of all net values calculated on the contract lines. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public decimal netprice
        {
            get 
            {
                return MoneyPropertyValue(CrmStr.netprice);
            }
            set
            {
                AddMoneyProperty(CrmStr.netprice,value);
            }
        }

        /// <summary>
        /// Gets the base-currency equivalent for the sum of all net values calculated on the contract lines. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public decimal netprice_base
        {
            get 
            {
                return MoneyPropertyValue(CrmStr.netprice_base);
            }
        }

        /// <summary>
        /// Gets the total price of the contract. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public decimal totalprice
        {
            get 
            {
                return MoneyPropertyValue(CrmStr.totalprice);
            }
            set
            {
                AddMoneyProperty(CrmStr.totalprice,value);
            }
        }

        /// <summary>
        /// Gets the total price base of the contract. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public decimal totalprice_base
        {
            get 
            {
                return MoneyPropertyValue(CrmStr.totalprice_base);
            }
        }

        /// <summary>
        /// Gets the total of all discounts specified on the contract lines. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public decimal totaldiscount
        {
            get 
            {
                return MoneyPropertyValue(CrmStr.totaldiscount);
            }
            set
            {
                AddMoneyProperty(CrmStr.totaldiscount,value);
            }
        }

        /// <summary>
        /// Gets the base-currency equivalent of the total for all discounts specified on the contract lines. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public decimal totaldiscount_base
        {
            get 
            {
                return MoneyPropertyValue(CrmStr.totaldiscount_base);
            }
        }

        /// <summary>
        ///  Gets or sets a value that indicates whether the discount is a percentage or a monetary amount. 
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public bool usediscountaspercentage
        {
            get 
            {
                return BooleanPropertyValue(CrmStr.usediscountaspercentage);
            }
            set
            {
            
                AddBooleanProperty(CrmStr.usediscountaspercentage,value);
            }
        }
        
        /// <summary>
        /// virtual because on 'detail' records it is only a uniqueidentifier property
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public Guid owningbusinessunit
        {
            get
            {
                return LookupPropertyValue(CrmStr.owningbusinessunit);
            }
            set
            {
                AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value);
            }
        }

        public string owningbusinessunitName
        {
            get
            {
                return LookupPropertyName(CrmStr.owningbusinessunit);
            }
        }

        /// <summary>
        /// Owner for this object
        /// </summary>
        [CrmDynAttribute(CrmStr.contract)]
        public override Guid ownerid
        {
            get
            {
                if ( ContainsKey(CrmStr.ownerid) )
                    return OwnerPropertyValue(CrmStr.ownerid);
                return Guid.Empty;
            }
            set
            {
                AddOwnerProperty(CrmStr.ownerid,CrmStr.systemuser,value);
            }
        }

        #endregion Pricing
    }
}
